package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.biz.JsNotesBiz;
import com.cxf.windex.entity.JsNotes;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @author chenxf
 * @date 3/16/2018
 * @description
 */
@Api(tags = {"小程序 - 意见反馈接口"})
@Controller
@RequestMapping("/user/notes")
public class NotesCotroller extends BaseRestController<JsNotesBiz, JsNotes> {

  @ApiOperation(value = "查询账本记录", notes = "查询账本记录")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "orderStr", value = "排序 desc", required = false, paramType = "query", dataType = "String"),
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<JsNotes> list(
          @RequestParam(defaultValue = "10") int limit, @RequestParam(defaultValue = "1") int offset,
          @RequestParam(defaultValue = "desc") String orderStr) {
    Example example = new Example(JsNotes.class);
    Criteria criteria = example.createCriteria();

    int count = baseBiz.selectCountByExample(example);
    PageHelper.startPage(offset, limit);
    PageHelper.orderBy("crt_time " + orderStr);
    return new TableResultResponse<JsNotes>(count, offset, limit,
        baseBiz.selectByExample(example));

  }

  @ApiOperation(value = "新增/编辑/查看", notes = "新增编辑查看")
  @RequestMapping(value = "/edit", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse edit(JsNotes entity) {
    if (StringUtils.isBlank(entity.getId())) {
      return new ObjectRestResponse().rel(true).msg("意见反馈ID不能为空");
    }
    entity = baseBiz.selectById(entity.getId());
    return new ObjectRestResponse().rel(true).msg("查询成功").result(entity);
  }

  @ApiOperation(value = "保存", notes = "保存")
  @RequestMapping(value = "/edit", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse save(@RequestBody JsNotes entity) {
    String type = "edit";
    if (StringUtils.isBlank(entity.getId())) {
      type = "insert";
      entity.setId(new IdUtil(3, 1).nextId() + "");
    }
    String userId = this.getCurrentUserId();

    if ( userId==null || userId.equals("") ) {
      //return new TableResultResponse<Address>(0, offset, limit,new ArrayList<Address>());
      userId="1";
    }
    entity.setUserid(userId);
    if (type.equals("insert")) {
      baseBiz.insertSelective(entity);
    } else {
      baseBiz.updateSelectiveById(entity);
    }
    return new ObjectRestResponse().rel(true).msg("保存成功");
  }
}
